package com.lhy.mymall.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import com.lhy.mymall.common.Constant;
import com.lhy.mymall.service.EmailService;
import com.sun.xml.internal.ws.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author: 卢辉宇
 * @date: 2023/1/13 17:27
 * @description: email的实现类
 */
@Slf4j
@Service
public class EmailServiceImpl implements EmailService {

    //注入java的发邮件对象
    @Resource
    private JavaMailSender mailSender;

    //注入Redis对象
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * @param to      发送给谁
     * @param subject 主题
     * @param text    内容
     */
    @Override
    public void sendSimpleMessage(String to, String subject, String text) {
        /*实例化spring的邮件内容的封装对象*/
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //给对象设置发件人(自己，在常量类中设置)
        simpleMailMessage.setFrom(Constant.MAIL_FROM);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(text);

        log.info("邮件验证码："+text);
        //使用发邮件对象发送
        mailSender.send(simpleMailMessage);
    }

    /**
     * 把邮件验证码暂存到redis中
     *
     * @param emailAddress
     * @param verificationCode
     * @return 只有返回true才能正确缓存到redis中
     */
    @Override
    public boolean saveEmailToRedis(String emailAddress, String verificationCode) {
        /*********************redison的操作*****************************/
//        //得到redis的客户端对象（默认连接到本机的redis）
//        // 创建配置 指定redis地址及节点信息
//        Config config = new Config();
//
////        config.useSingleServer().setAddress("redis://192.168.42.130:6379").setPassword("lhy123");
//        //通过常量类来获取redis
//        config.useSingleServer().setAddress("redis://"+Constant.REDIS_HOST+":"+Constant.REDIS_PORT).setPassword(Constant.REDIS_PASSWORD);
//
//        // 根据config创建出RedissonClient实例
//        RedissonClient client = Redisson.create(config);
//        //获取redis容器
//        RBucket<String> bucket = client.getBucket(emailAddress);
//        boolean exists = bucket.isExists();

        /********************SpringDataRedis的操作*/
        String email = stringRedisTemplate.opsForValue().get(Constant.LOGIN_CODE_PRE+emailAddress);
        if (StrUtil.isBlank(email)){
            //里面不存在emailAddress的内容，放入内容,60秒有效期
            stringRedisTemplate.opsForValue().set(Constant.LOGIN_CODE_PRE+emailAddress,verificationCode,60, TimeUnit.SECONDS);
            return true;
        }
        return false;
    }

    /**
     * 检查传进来的验证码和redis中的验证码是否一致
     *
     * @param emailAddress
     * @param verificationCode
     * @return
     */
    @Override
    public boolean checkEmailAndCode(String emailAddress, String verificationCode) {

        // 创建配置 指定redis地址及节点信息
        Config config = new Config();
//        config.useSingleServer().setAddress("redis://192.168.42.130:6379").setPassword("lhy123");
        //通过常量类来获取redis
        config.useSingleServer().setAddress("redis://" + Constant.REDIS_HOST + ":" + Constant.REDIS_PORT).setPassword(Constant.REDIS_PASSWORD);


        // 根据config创建出RedissonClient实例
        RedissonClient client = Redisson.create(config);
        //获取redis容器
        RBucket<String> bucket = client.getBucket(emailAddress);
        boolean exists = bucket.isExists();
        if (exists) {
            //里面存在emailAddress的内容
            String code = bucket.get();
            //比较两者是否一致
            if (code.equals(verificationCode)) {
                return true;
            }
        }
        return false;
    }
}
